﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class EditProfile : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User user;
    Country country;
    State state;
    List<State> states = new List<State>();
    List<Country> countries = new List<Country>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountries();
            BindStates(1); //Note: By DEfault we load US States.

            if (Session["User"] != null)
                FillDetails(Session["User"].ToString());
            else
                Response.Redirect("default.aspx");
        }
    }
    public void FillDetails(string userName)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = userName;
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            txtFirstName.Text = nUser.FirstName;
            txtLastName.Text = nUser.LastName;
            txtEmail.Text = nUser.Email;
            txtPhone.Text = nUser.Phone;
            txtAddress1.Text = nUser.Address1;
            txtAddress2.Text = nUser.Address2;
            drpCountry.SelectedValue = nUser.Country.ToString();
            drpState.SelectedValue = nUser.State.ToString();
            txtCity.Text = nUser.City;
            txtZip.Text = nUser.Zip;
            drpPropertyLocation.SelectedValue = nUser.PropertyLocation;
            drpServiceRequired.SelectedValue = nUser.ServiceRequired;
            txtUsername.Text = nUser.UserName;
            txtUsername.Enabled = false;
            txtPassword.Attributes.Add("value", nUser.Password);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = Session["User"].ToString();
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            nUser.FirstName = txtFirstName.Text;
            nUser.LastName = txtLastName.Text;
            nUser.Email = txtEmail.Text;
            nUser.Phone = txtPhone.Text;
            nUser.Address1 = txtAddress1.Text;
            nUser.Address2 = txtAddress2.Text;
            nUser.Country = Convert.ToInt32(drpCountry.SelectedItem.Value);
            nUser.State = Convert.ToInt32(drpState.SelectedItem.Value);
            nUser.City = txtCity.Text;
            nUser.Zip = txtZip.Text;
            nUser.PropertyLocation = drpPropertyLocation.SelectedItem.Value;
            nUser.ServiceRequired = drpServiceRequired.SelectedItem.Value;
            nUser.UserName = txtUsername.Text;
            nUser.Password = txtPassword.Text;

            nUser.UpdateUserProfile();
            Session["myAccount"] = "Profile updated successfully.";
            Response.Redirect("editProfile.aspx");
        }

        FillDetails(Session["User"].ToString());
    }
    private void clearControls()
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtAddress1.Text = string.Empty;
        txtAddress2.Text = string.Empty;
        drpCountry.SelectedIndex = -1;
        drpState.SelectedIndex = -1;
        txtCity.Text = string.Empty;
        txtZip.Text = string.Empty;
        drpPropertyLocation.SelectedIndex = -1;
        drpServiceRequired.SelectedIndex = -1;
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }
    private void BindCountries()
    {
        country = new Country(_dataObject);
        countries = country.GetAllCountries();

        drpCountry.DataSource = countries;
        drpCountry.DataValueField = "Id";
        drpCountry.DataTextField = "Name";
        drpCountry.DataBind();
    }
    private void BindStates(int countryId)
    {
        state = new State(_dataObject);
        states = state.GetStatesByCountryId(countryId);

        drpState.DataSource = states;
        drpState.DataValueField = "Id";
        drpState.DataTextField = "Name";
        drpState.DataBind();
    }
}